package com.logicalthinking.socialmall.service.impl;

import com.github.chenlijia1111.utils.common.Result;
import com.github.chenlijia1111.utils.core.PropertyCheckUtil;
import com.github.chenlijia1111.utils.core.StringUtils;
import com.logicalthinking.socialmall.entity.SystemConfig;
import com.logicalthinking.socialmall.dao.SystemConfigMapper;
import com.logicalthinking.socialmall.service.SystemConfigServiceI;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 系统设置表
 * @author chenLiJia
 * @since 2020-02-17 14:43:59
 **/
@Service
public class SystemConfigServiceImpl implements SystemConfigServiceI {


    @Resource
    private SystemConfigMapper systemConfigMapper;


    /**
     * 添加
     *
     * @param params      添加参数
     * @return com.github.chenlijia1111.utils.common.Result
     * @author chenLiJia
     * @since 2020-02-17 14:43:59
     **/
    @Override
    public Result add(SystemConfig params){
    
        int i = systemConfigMapper.insertSelective(params);
        return i > 0 ? Result.success("操作成功") : Result.failure("操作失败");
    }

    /**
     * 编辑
     *
     * @param params      编辑参数
     * @return com.github.chenlijia1111.utils.common.Result
     * @author chenLiJia
     * @since 2020-02-17 14:43:59
     **/
    @Override
    public Result update(SystemConfig params){
    
        int i = systemConfigMapper.updateByPrimaryKeySelective(params);
        return i > 0 ? Result.success("操作成功") : Result.failure("操作失败");
    }

    /**
     * 条件查询
     *
     * @param condition  
     * @return 
     * @author chenLiJia
     * @since 2020-02-17 14:43:59
     **/
    @Override
    public List<SystemConfig> listByCondition(SystemConfig condition){
    
        PropertyCheckUtil.transferObjectNotNull(condition, true);
        return systemConfigMapper.select(condition);
    }


    @Override
    public SystemConfig findByKey(String key) {
        if(StringUtils.isNotEmpty(key)){
            SystemConfig systemConfig = systemConfigMapper.selectByPrimaryKey(key);
            if(Objects.isNull(systemConfig)){
                systemConfig = new SystemConfig();
                systemConfig.setConfigKey(key);
                systemConfig.setConfigValue("");
                systemConfigMapper.insertSelective(systemConfig);
            }
            return systemConfig;
        }
        return null;
    }
}
